Improved how entity path is computed.

Also made code object-oriented and ready for inheritance.