Fix some bugs in the program and add some extra debug