code cleanup and formatting
performance improvements sorting tree elements