untangled spaghetti code with nested functions, also used `print()` for
python3.