modulized the whole code