Fixed indexing bug and added tests.