Completly revised version of qi.php

Some changes to your last commit