             // We are guaranteed that we have only one positive root, since d >= b:
             var rowCount:Number = (a != 0) ? (b + d) / (2 * a) : elementCount;
 
-            var row1:int = Math.max(1, Math.round(rowCount));
+            // To get integer count for the columns/rows we round up and down so
+            // we get four possible solutions. Then we pick the best one.
+            var row1:int = Math.max(1, Math.floor(rowCount));
             var col1:int = Math.max(1, Math.ceil(elementCount / row1));
-
-            // Now try to reduce the bigger dimension and see if we can come up with a better distribution
-            var row2:int;
-            var col2:int;
-            if (col1 * (_columnWidth + hGap) - hGap > row1 * (_rowHeight + vGap) - vGap)
+            row1 = Math.max(1, Math.ceil(elementCount / col1));
+
+            var row2:int = Math.max(1, Math.ceil(rowCount));
+            var col2:int = Math.max(1, Math.ceil(elementCount / row2));
+            row2 = Math.max(1, Math.ceil(elementCount / col2));
+
+            var col3:int = Math.max(1, Math.floor(elementCount / rowCount));
+            var row3:int = Math.max(1, Math.ceil(elementCount / col3));
+            col3 = Math.max(1, Math.ceil(elementCount / row3));
+
+            var col4:int = Math.max(1, Math.ceil(elementCount / rowCount));
+            var row4:int = Math.max(1, Math.ceil(elementCount / col4));
+            col4 = Math.max(1, Math.ceil(elementCount / row4));
+
+            if (closerToSquare(col3, row3, col1, row1))
             {
-                col2 = Math.max(1, col1 - 1);
-                row2 = Math.max(1, Math.ceil(elementCount / col2));
+                col1 = col3;
+                row1 = row3;
             }
-            else
+
+            if (closerToSquare(col4, row4, col2, row2))
             {
-                row2 = Math.max(1, row2 - 1);
-                col2 = Math.max(1, Math.ceil(elementCount / row2));
+                col2 = col4;
+                row2 = row4;
             }
 
-            // Pick the better one of the two
-            if (Math.abs(col1 * (_columnWidth + hGap) - hGap - row1 * (_rowHeight + vGap) + vGap) <=
-                Math.abs(col2 * (_columnWidth + hGap) - hGap - row2 * (_rowHeight + vGap) + vGap))
+            if (closerToSquare(col1, row1, col2, row2))
             {
                 _columnCount = col1;
                 _rowCount = row1;

                _columnCount = Math.max(_columnCount, Math.ceil(elementCount / Math.max(1, explicitRowCount)));
        }
    }

    /**
     *  @private

    //  justifyColumns
    //----------------------------------

    private var _justifyColumns:String = TileJustifyColumns.NONE;

    [Inspectable(category="General", enumeration="none,gapSize,columnSize", defaultValue="none")]

    /**
     *  Specifies how to justify the fully visible columns to the container width.
     *  ActionScript values can be <code>TileJustifyColumns.NONE</code>, <code>TileJustifyColumns.GAPSIZE</code>
     *  and <code>TileJustifyColumns.COLUMNSIZE</code>.
     *  MXML values can be <code>"none"</code>, <code>"gapSize"</code> and <code>"columnSize"</code>.
     *
     *  <p>When set to <code>TileJustifyColumns.NONE</code> - turns column justification off, there may
     *  be partially visible columns or whitespace between the last column and
     *  the right edge of the container.  This is the default value.</p>
     *
     *  <p>When set to <code>TileJustifyColumns.GAPSIZE</code> - the <code>horizontalGap</code>
     *  actual value will increase so that
     *  the last fully visible column right edge aligns with the container's right edge.
     *  In case there is only a single fully visible column, the <code>horizontalGap</code> actual value
     *  will increase so that it pushes any partially visible column just beyond the right edge
