corrected some bugs, but currently the program deadlocks