Tweak core.serialize

This adds proper support for nested tables and improves performance a bit.
It also makes it follow the code style guidelines.