Fixed some type issues and added protected for nil in returning direction map