This code took me so long to debug... :P