Branch selection function code improvement

Thanks to Nicofuma