Integrated from Davide's patch.