more or less finished simulation function