Refactored layout code to allow subclassing