Moved all of the error handling to a separate function that takes 2 parameters (error, functionname) to centralize the error handling.
Improved readability by inverting some of the if statements
moved some of the more complex functions into smaller inner functions
Moved all of the error handling to a separate function that takes 2 parameters (error, functionname) to centralize the error handling. Improved readability by inverting some of the if statements moved some of the more complex functions into smaller inner functions