Restructured and somehow modulized the code small