This whole thing needs some clean-up and refactor. I will try to get to that as soon as I can