Removed some DISTINCT queries and more joins on id values instead of varchars