Create task after setting up listener, and some refactoring