Fix bug in perl implementation
make ruby simpler