Make perm encoding return a maybe (cause it uses malloc, which can fail)