Updated code with refactoring.