IP.py: fixed bugs, shortened code