added docs and cleaned up code