clean up, added docs, reviewed edge cases