Embedded the snippet in code with useful comments (I hope)