Refactor rendering

merge this after #268
Use memoize?