Fixed #842 and added remaining changes for #860