simplify some code, functions now store state in closures