general bug fixes and optimization