Major code refactoring part 2.