Refactor, test, and simplify