Looks good. Can you perhaps extract the logic into a well-named method(s)?