Refactor some code and add start-end logging to tasks.