Fixed queries, made code neater