Completed coding for first pilot simulation