Shamelessly ripped from Panopoly but dropping so many dependencies.