refactored rendering code