Big refactoring to a more sensible layout