bugs fixed; output seems to match before refactor