seems to be working, needs cleaning up and tests