first refactoring, still a lot to do