Would it be possible to break the chunks inside this block into more concrete functions? Might make testing easier as well.