refactor main dispatch