improve sql handeling