Refactoring and minor changs.