Looks like there is quite a bit of code duplication going on in both cases.