Fixed BW algorithm, added a test