Rename `type` to `source`
Reorganize code