tried to integrate Tom's changes fully