improve BF algorithm implement code