Refactoring and improved type conversion