refactoring, quite a bit