rewrote json function and fixed queries