Test and fix for badly formatted IMS, and optimised variable usage