Simplified code with conditionals in main loop.