few improvements and refactoring