First complete version of factor.py