Refactor classes, use memoization