Fix the cardinal direction algorithm