"I copied this and it works, but I'm not 100% sure why"
If you don't know why, deconstruct it until you do. Otherwise how can you ever debug it?
But you MUST understand the principles on which it works. Otherwise you're just creating delayed pain for yourself and others down the line.