implementing the rendering algorithm