Refactored automate