Rewrote in perl because perl is better