Added safety checks to prevent excecptions