Fixed up error reporting and cleaned up SQL inserts to prevent syntax errors.