Added a bunch of error checking and unit testing logic