refactor and clean code for Problem 4