added debug and fixed some data validation checks