My somewhat unelegant solution