reorganize functions & clean up