Added some protections into the code that prevent errors when we don't get what we expect from the inputs.