Lots of bug fixes and minor changes