Correcting some bugs and deleting some duplication.