Problem 23 solved, albeit inefficient