Added proper reference detection