working on the algorithim, but this is going to need a lot of cleaning up and optimixing eventually