various improvements and refactorings