Rewritten and refactored generics code for instance generation