Fixed some bugs, doc, removing debug messages. Should be working OK.