applied a fix to mag.cpp from Myunghwan