a bit more refactoring and testing