Repair some bugs and optimalize little bit.