cleaned up code and added error handling