Lots of basic bug fixes