add state tracking to prevent multiple opens/closes
cleanup comments