Some refactoring for code quality, lessening complexity in core functions