refactored getComponentInDirection method