Refactor rendering

merge this after #268