Cleaned up encode logic and exposed additional fields for external usage