Added test for function serialization and fixed related bugs.