optimize task dispatching logic