added writeResponse function