The mechanism of the Bamberger rearrangement