Added logic for empty grid