code cleaning, error handling fixed