Refactoring and a small (but important) bug.