Added lots of sanity checking