ported implementatioin to php 5.3