merge some changes from @AdrianoMartins