Refactored the code substantially to hanlde common layouts