Merged Jouni's operation mode code.