Refactoring build function