Argh, hopefully got the logic right this time...