First (rather messy) attempt