Fixed #6481 -- Fixed a bunch of import problems (and some whitespace cleanups). Found by Bastian Kleineidam with help from pyflakes. Thanks.