Cleanup filtering and marshaling code

Hopefully this will make it easier to improve.