Added some checking and logging