refactored tests and fixed two offby O(n) errors