reduce the complexity of the queries
changed sed command