improved the code for bwt