Fix type bug and some indexing bugs.