Some bug fix

Here are some fix for the bug I found during development.