query improvement

logic cleaning has to be done