Rework layouting algorithm