Refactored, added reusable functions & comments