Changed scope of layer variables to global