Refactored to clean up the code a bit for when I reference it in my article