Updated after comparing to the original code this came from