near final code, added lots of docs