Is there a reason for this code-duplication? at least the 5 first lines seem to be identical.