Formatted code to pep8, moved imports outside of function.