Corrected implementation for heading detection.