Refactoring, moved all task handling code to task.py