Added changes from #4041 and #4069. Thanks @infograf768!