Redone from scratch, hope I got it right this time