clean up code
added error handler