refactored script to setup visibility detection