optimized both of the queries