function for handling data almost finished