Fixed various important bugs.