Fixed null pointer exception. Also minor bugs.