Refactored/cleaned the code for example4