Improve generics handling

Fixes #750