Part 2 solution