several fixes for scoping