Rewritten in ruby