add code from Emanuele for new implementation