Needs a massive refactor. But should be way more efficient