Refactor for more idomatic Python