edited layout algorithm slightly