improved end detection
compile