Changed handling of number of rows and columns to be more efficient