code from gray-hat-python