some bug fixes and reorganization