Good thinking T@r@r.. don't waste your time and money fixing the B20... I did and I regretted it.
If you want to swap your engine it's easier to get a halfcut so that you can use the ECU and wiring, breaks and exhaust, etc. If you don't have the money and you don't mind the M50b25, all you need is the engine, intake manifold, breaks and exhaust (maybe optional). I did that and it cost me around ~4K including workmanship. I'm pretty happy with the results.
It's definitely possible to use an e39 engine but I don't know how much that'll cost.