Fixed bugs in local detection. Speed improvments as well