doing some SQL imrovements