This is a lot of logic for one method.  It could benefit from being broken down into separate helper methods that construct the individual pieces.