More refactoring and commenting