Some minor changes explained in comments