Added a lot of debug info and corrected some of the validity checking