Added profilingtests.
Minor performance improvements and code cleanup.