Added query code and some functions