Major code refactoring part 4.