Made a few changes to Jay's original code