Fix bug in teishi.c, add two helper functions.