optimize and reformat loading indicator code