removed has_key and oter cleanup
cleanup nested query generation