Refactoring and small enhancements