Rewrite to use promises