Still refactoring problem 4 in Python