Add restart handling and corrected a few bugs