cleaned up the main method and brought functions outside of it