Bug fixes

Fixed 3 major issues.  Program seems to be emitting at least sort of
correct output now, but more testing is needed