Fixed various bugs introduced by previous commit.

Signed-off-by: Xiaomao Chen <xiaomao5@live.com>