changes to handle != null query and making query optional