Fixing an edge case in belongs-to function