Refactoring of data selection, correction of query