Fixed bug with ap/jpg mode switching. Cleanups.