Improved way in which tasks are cancelled

Added cancel method by task id.
Bunch of fixes and refactorings.