Merged hugomgs fork and mine

although prolly out of date now, it shouldnt take long time to merge to
newer versions. Also has some tweaks and fixes.