Added advanced error checking.