refactored some code and finished commenting