Refactored code to CreateTask factory method