rewrite query5 api impl for slightly more efficiency (i hope)