Moved some code in render function to separate functions to make more readable.