Still working on error checking but fixed a bug introduced while doing so