added extra checks and corrected a error