Updated with comments to help with understanding it.