Add null checks and cleanup print statements