Revert "Refactored problem 4 to be nicer". Error in logic that was not spotted.

This reverts commit ef7a22175d300ad5f1569a83a5e4a4284e496ab5.