move function api to top
reorganize code