Refactor libmn.cpp