Not directly related to this PR, but I'd recommend to put this into a separate method. It is quite isolated, and might be usable somewhere else as well.