move heavy functions to ruby, add memoization