non-io code called with mex

TODO - io version of code