Quite complex case analysis for 270, took me far too long to debug... (runs in 0.3s)