Add tests with perft. Fix some bugs.