various improvements and refactoring