Added a generic version

This new version resolves a few small bugs I found in the current
non-generic version as well...