Part 2 ported to Ruby