I made a hash of this code, but it works