added docs and did some major refactoring