Enhancing Magic Sets with an Application to Ontological Reasoning