Refactor gesture recognition