refactored, compiles untested