apply patch code provided by Giulio Paci.