mostly working, needs error checking