Adding additional checks to prevent crashes