merging pr from @hzr #1 with additional fixes