improved branch handling