refactoring, pretty close