Removing dead code, some refactoring, and error checking on secondary array.