problem 5 refactored (python)