Fix userdata implementation and add relevant new tests.