(slow) Solution for problem #7