reorganize to allow to run in matlab