Instead of listening to requests for each window separately, add a listener for all requests. Also added some comments.
Added more comments to make the intent of each function clear.