refactored layout code