Refactor all codes from groove.py