refactored, but not tested