copied implementation from igor project, tests are passing now