some corrections and refactoring