Own algorithm fixed to avoid deadlock