Ported some more from Python2 to 3. Made changes to a function for clarity