Several bug fixes, small style edits