Code refactoring, adding comments