Improved the REM-detection algorithm.