Fix some bugs when generating codes for function