Refactor of level flushing code

Move common code for flushing rep & def levels into their own function.