refactor the build function