Moved logic out of drawing function.