Added a bunch of error checking and TODO's