Trying to improve the dynamic maxDirectionChanges implementation