attempt to make api-creation code more robust and leak-proof