Refactored a bit
Fixed mem leak in render()