Several bug fixes in the last commit.