Some general bug fixes