added a little of Francesco's code back