modified output and cleaned up code from original