It seems like this "checking" pattern is repeated a couple times over the code, if you can think of a way to avoid the code duplication, that would be awesome but if you think this has to stay as is that is fine as well.