Major refactoring + move non-lib code to example.py