Refactor and small enhancements