completed refactoring and commenting