works, but a steaming pile of code
cleaning up