Tidied up simulation code and added validations.