better implementation of task function