document more functions. move functions to utils file. simplify.