Python 2 AND 3 compatibility.
Modularized internal function.