A few things added to get Antonio's algo working