Rewrote serializer to reduce complexity and added unit tests