Myles' code answer for leetcode