Fix return codes and minor fixes