Finished refactoring and adding comments