Fix the bugs appeared after last PR

Also, please try to separate code formatting commits from code changing commits, otherwise it is almost impossible to detect changes in the code that are "masked" by code formatting changes (like indentation).