Complete rewrite by Mighty Pork