Some heavy refactoring.