add debug prints, and fix a few bugs ;)