Cleanup the BTree logic so it does not use global calls

Also made some methods private and added comments.