update code added for flexibility and octave efficiency