Improved simulation function.