refactored, untested can be used as a module