made it runnable from a program
added several checks
updated according to (e)pylint and pyflakes suggestions