This does very minor optimization and some cleanup on the perl implementation

It's still grotty, but well.