some SQL improvements