Solve problem #10, in need of optimization