Fixed some bugs. Lots of TODOs fixed.