Adding defensive checks.
more informative logging