Refactored code to sidestep bug.