deleted flag fix, minor optimizations