Refactoring to simplify layout code. Adds macros too.