first working version

commit first working version. function not fully tested yet.