Added lots of debug and various fixes