Refactored ref impl, fixes #3