added comments, refactored