Refactored code to better handle schemas