This is a straight refactor. I'm working with this code and cleaned it up to make it easier to work with.