Some impreovments and removing the query code to put it in the frontend.