Optimize 'detectGestures' funcition