Fixed quite a few bugs.