Now simulation exits correctly. Code cleanup.