major refactoring.

still need to test in METaL