[Refactor] Converted inner function to outer function (minor perf benefits?)
Plus improved comments