reviewed version. some issues remaining