Completely restructured main method. It should work now