Brought over some changes from chipbot