Modified writing to database (Incomplete)