Moved paint loop to its own function