refactored expand.py