Added safety checks and logging capability.