fixed bugs, needs factoring to work still.