Refactor wraith to do all tasks in series and close the DB connection at the end