fixed a bug with scope cleanup