a roughly working version of the program. It does need debugging