Refactor layout logic into layout controller