split code into main method and a single function