I've shortened this part by removing the long example code and rearranging things a bit to improve clarity.