Cleanup: various improvements to layout code