copied from freevms and modified