Adapted https://github.com/sehe/Bartek/pull/1