Finished the sim function