refactored a lot of the code, improved docs, tested.