Refactoring Automata