briefly refactored (not tested)