I gave up trying to bash other people's solutions into shape and made my own.