Linux/MacOSX: code refactoring and cleanup.