we should document this overall code here, i don't quite follow it