Almost all done

Works, needs cleanup and more comments.