The resizing code here is exactly duplicated from above. You can abstract this to a function.