Eeew, there's got to be a better way to do this, including (but not limited to), rewriting this whole method in Ruby :)