Feature/1216 tern support

Code for issue #1216

@remy Reopened after I've fucked up the other one.