Fix several bugs and typo
Bug fixes