many bug fixes and reorginzations