improved the performance of this by a whole lot by just using joins