very ugly code, hardly shout out f**k