Working code for assigning and updating new layer.

Working code for assigning and updating new layer.
Next step: Get it working for more than one layer in the main function.
Need to update gCount in the original function.