dispatch refactoring and adding tests