refactoring bwt.erl