the block below looks like another good candidate to be put in separate functions...