Week 4 solutions refactored after Pavel's comments