Updated to work in ruby 1.8.7 and major re-write