Fixed some bugs and tested this to work.