Pretty happy with this solution. Took me longer to read and understand part 2 than I would have liked, but it's done, and pretty cleanly, too.