Fixes the bug reproduces from the last two examples and finishes the refactoring.