first working version; probably still many bugs