Removed dead code & added safety checks