Wrapping it all up...
OK, we've got the basics. We know about the layer, we understand the loops, now let's put it all together in a function. Yes, I know there are two functions, but look again
(Maximum characters: 1200). You have characters left.