added docs, refactored some code