extract getDirection function