Improved layout logic