Import and tidy up macro from Gist