first working version, pretty complete