added comments and simplified code