code refactoring for getlayout function