Use encode_to_array to avoid extra copies
Improve timeout specifications
Handle nil timeout, since system_timer does not