Major refactoring of rendering logic