Solved off-by-one error. cleaned up code