massive refactoring to get it ready for dynamic command dispatch