finished version. may add comments later for future reference. this version passed every test case, even style.