first part of #119 merged by ggrnd0
second part of #119 merged by ggrnd0