changed the order of function to write and added function name to header