Fixed some potential code duplication issues with regard to graceful termination