Ugliest code of my life