removed unused code and streamlined subqueries
reworked logic, hoepfully will reduce blocking