reorganize code in main file