Lots of edits, mostly bug fixing