fixed error handling/code clean up