Lots of bug fixes from code inspection