modularized main function
more modularized (by reducing scope of some variable)
improved usage of table in loop statement (by reducing its hierarchy)