Refactor build function to be more readable