May be move this statements outside cycle? Do you forget reverse string?