first working version. Needs testing