Add updated logic for detecting visibility