fixed writing to database