Refactor layout engine

Cassie - please do CR.