Encoding function written. Need to debug.