Several minor bug fixes