Finished query code

Do not merge this one I need to code clean up and will check in a newer branch