refactored simulation code to use indices