More refactoring and updates

I need to work on the command line interface but the functions are
pretty much where they need to be.
Bug fix and started adding tests