Lots of bug fixes