Amended code as requested by nprosser