Add safety checks and some comments