copied over logic from vruby