Not very elegant, but it works