Finished program with all loops replaced