Refactoring

Some more refactoring in preparation for Cairo work.