general bug fix and clean up