Refactor the direction selecting code