working on database write function