Fixed nested scopes and optimizing (sorta)