Refactor amalgamate.py