Fixed a minor bug by replacing it with stronger code and better error
messaging