fixing infinite loop issue
fixed REM bug