Totally messy code. Will clean it up tomorrow