Some refactoring and some logic changed.