upadate code to work with the database. Still needs finishing