further refactoring and commenting