Replaced original implementation with HN user grayrest's code, as quoted at the end of the original blog post, and fixed the webserver stuff.