Refactor code from the tasks controller