total spagetti mess, but works. will refactor eventually