Fixed some bugs. Seems to work.