Changing query order/index for better performance. Removing more unnecessary code