Minor Refactor, pulling some nested functions apart.