Refactor layout engine

Don't merge it yet ;)