Removed extra reads and writes.
Adjusted memoization.
Adjusted formatting and comments.