Code:  Refactoring and minor fixes