Fixed duplicated error/warning messages, fixed reference handling bugs