Improved target handling, fixes test failures.