JZ: Some refactoring and fixes for #622