added comments and error handling