Refactoring by @pengwynn