Improved and cleand-up BP code