Added most of the error-checking functionality