Some more refactoring and re-arranging