Once again reorganized code to read more logically