Refactor to avoid monkeypatching.