Started refactor of mgen-lib