forked, modified and cmaked code from FADBAD 1.4