Just did some quick pseudo coding.