Refactor layout engine