changes inspired by xtine's fork but with some tweaks