added direction detection (unrefactored)