encoder code revised for readibility