Fixed several bugs and increase the speed.