I updated the snippet to support GIFs and fix a few bugs. The new code has more error checking and more comments.