Big rewrite to use promises instead of callbacks.