moving relevant code from old version