Refactor with Q.ninvoke