Refactoring work in progress. Still a lot left to be done.