adding debug code and fixing bug (finally?)