Improve sql handling