Changed sql to use correct tables