first working version, fully documented