Refactored, added comments