fixed several serious bugs.