added comments and clarified code