fixed some issues related to return codes and closed output streams