Fix a bug on INSTANTIATE function and add corresponding unit tests

This was due to incorrect propagation of errors.

The bug is related to #2.