Removed global variable pApi. Several tidy-ups.