Rework some bad logic, add utility function.