added comments and debugged