adding refined output writing to DB