Fix gradient rendering code so re-rendering works without crashing

The current code sometimes removes the wrong layer when re-rendering. This PR fixes that issue by storing the gradient layer in a property.