Fix branch rewrite logic