refactor code in base.py