Some bug fixing and pep8, pylint improvements