Refactoring changes merged with enrico and paolo.
Several fixes for bugs introduced by refactoring