Some severe refactoring