minor refactor and memoization for performance