Made several changes to the original code