Gesture detection code refactoring