Refactor, added docs, added tests