Updated sanity check
Updated and cleaned code -- added sanity checking