Added some error handling
minor fixes