This should probably be refactored to something else... I was in a hurry.