Added documentation and fixed a few bugs found while documenting.