Refactor to make code more Pythonic, part 3