Fixed some NPEs and type exceptions in the application code.