lots of function refactor