Refactor function into Helpers.cs and cleanup code.