changed order of saving in order to prevent errors and duplicates