Consolidate redundant code to build function

Add document and change API

fix doc