cleanup and some exception safety