breaking this huge function up into smaller bits