Some changes of logic and code-style fixes