refactored functionality with 2d indexes for location