Fixed several serious bugs.