code revised for readability