replace statement with connection on error reporting, few variables in wrong places