update error handling, clean up and formatting