Added database writing code