better layout algorithm (midpoint replacement)