refactored, added comments