Cleaned up code.

Better pep compliance and more exact imports. Changed .has_key() to in.
Added docstrings.