Refactoring and fixed some type conversion code for arrays with primitive component type