So this is the program, so far it appears to work well.