Refactor mdlib