made some code null-safe
fixed code formatting