improving impl
misplaced vm
basic formulation for general case