Added James' solution as a base and expanded upon it.