Some code refactoring and several changes.