minor corrections and refactoring