Fixed some problems. Seems to work with limited testing.
Fixed all problems for now, but still needs a lot of work.