initial code for the program, roughly adapted from the flux program