more refactoring and commenting