Some more code refactoring before merging to master.