updating and cleaning up this spaghetti garbage code, now it is somewhat legible