pull request 2 integrated with main tree.  Main contributions from Lorenzo Fili and Samuele Dell'Angelo