My attempt at altering Max's code