working code, mid refactor but working