It looks to me like the content of this block and the one in the `if` could be pretty easily be refactored into a single method, It could take a list, and the plot to set the boundaries on.