this is the part I'm refactoring as it's ugly and slow.