added branching to the layout algorithm