added Detect from Paul's code. Will probably need further alterations