[how can I avoid it in the future]Maybe pay for one of the Pro versions of a good Anti-Malware program.They give you full time protection and less chance of picking up an infection. MalwareBytes is a one of payment I believe. I use Emisoft Anti Malware which I think is great but others may not agree.Or there is Superantispyware or new kid on the block Hitman Pro.No use using the free versions as they do not give you full time protection. I opened a post yesterday on here giving the pay for version of Mabutu free until tomorrow.So maybe try that if funds are a bit low.
iMac Pro review