fixes and refactoring of BBT code