Refactoring, added comments