Changed logic for saving data to database