Code and documentation cleanup, fix a few bugs