Refactor lib/ and basic.py