Refactor: code cleanup and reorganization of the  and related helpers.