Fixed several bugs and implemented MainTest.