minor corrections, more refactoring