Add tests and fix indexing problem