Optimized bf algo, running time down to 1500ms