merged optimization code from master to EDMdev branch