Added sanity check and logging