Extended functionality.
Split functions for export.
Trace output added.