Big refactoring has started. Ugly at the moment though.