Added more general grid_position function