fixed unflipped matrix issue... code is messy though