Fixed several critical bugs.